/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */

#include <QtGui/QLayout>
#include <QtGui/QMouseEvent>
#include <QtGui/QLabel>
#include <QtGui/QCheckBox>

#include "RigidBodyControl.h"

namespace PQBuilder
{
	RigidBodyControl::RigidBodyControl(QWidget *parent,PQEngine::RigidBodyComponent* component)
	{
		_rigidBody=component;
		if(_rigidBody!=0){
			bool useGravity=_rigidBody->useGravity();
			bool isKinematic=_rigidBody->isKinematic();
			float mass=component->getMass();
			Ogre::Vector3 force=_rigidBody->getConstantForce();
			
			QGridLayout* grid=new QGridLayout(this);
			
			QLabel* labelMass=new QLabel(QObject::tr("Mass:"),this);
			_editMass=new QLineEdit(this);
			_editMass->setText(QString::fromStdString(Ogre::StringConverter::toString(mass)));
			
			QLabel* labelUseGravity=new QLabel(QObject::tr("Use Gravity"),this);
			QCheckBox* checkUseGravity=new QCheckBox(this);
			checkUseGravity->setChecked(useGravity);

			QLabel* labelIsKinematic=new QLabel(QObject::tr("Is Kinematic"),this);
			QCheckBox* checkIsKinematic=new QCheckBox(this);
			checkIsKinematic->setChecked(isKinematic);
			
			QLabel* labelForce=new QLabel(QObject::tr("Constant Force"),this);

			QLabel* labelForceX=new QLabel("X",this);
			QLineEdit* editForceX=new QLineEdit(this);
			labelForceX->setAlignment(Qt::AlignHCenter);
			editForceX->setText(QString::fromStdString(Ogre::StringConverter::toString(force.x)));

			QLabel* labelForceY=new QLabel("Y",this);
			QLineEdit* editForceY=new QLineEdit(this);
			labelForceY->setAlignment(Qt::AlignHCenter);
			editForceY->setText(QString::fromStdString(Ogre::StringConverter::toString(force.y)));

			QLabel* labelForceZ=new QLabel("Z",this);
			QLineEdit* editForceZ=new QLineEdit(this);
			labelForceZ->setAlignment(Qt::AlignHCenter);
			editForceZ->setText(QString::fromStdString(Ogre::StringConverter::toString(force.z)));
			
			grid->addWidget(labelMass,0,0);
			grid->addWidget(_editMass,0,1);

			grid->addWidget(labelUseGravity,1,0);
			grid->addWidget(checkUseGravity,1,1);

			grid->addWidget(labelIsKinematic,2,0);
			grid->addWidget(checkIsKinematic,2,1);

			grid->addWidget(labelForce,3,0,1,2);

			grid->addWidget(labelForceX,4,0);
			grid->addWidget(editForceX,4,1);

			grid->addWidget(labelForceY,5,0);
			grid->addWidget(editForceY,5,1);

			grid->addWidget(labelForceZ,6,0);
			grid->addWidget(editForceZ,6,1);

			this->setLayout(grid);
			
			
			connect(_editMass, SIGNAL(editFinished()), this, SLOT(editMass()));
		}

		this->setMaximumHeight(7*28);
	}

	void RigidBodyControl::editMass()
	{
		if(_rigidBody!=NULL){
			QString massText= _editMass->text();
			bool ok=false;
			float mass=massText.toFloat(&ok);
			if(ok){
				_rigidBody->setMass(mass);
			}else{
				_editMass->setText(QString::fromStdString(Ogre::StringConverter::toString(mass)));
			}
		}
	}

	RigidBodyControl::~RigidBodyControl()
	{
	}
	
}
